package com.bwie.service;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.domain.Good;
import com.bwie.domain.Shenhe;
import com.bwie.mapper.GoodMapper;
import com.bwie.mapper.ShMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @Author: Li sir
 * @Date: 2025/1/6 10:08
 * @Description:
 **/
@Service
public class ShserviceImpl implements Shservice {
    @Autowired
    ShMapper shMapper;

    @Override
    public R list(PageInfoVo pageInfoVo) {
        //2.根据分类id、审核状态、提交时间精确匹配、根据分类名称、店铺名称、审核人模糊匹配
        Page<Shenhe> shenhePage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<Shenhe> shenheLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (pageInfoVo.getKeyWord()!= null){   //根据分类id 根据分类名称
            shenheLambdaQueryWrapper.like(Shenhe::getFenlei,pageInfoVo.getKeyWord());
        }
        if (pageInfoVo.getTypeId()!= null){//审核状态
            shenheLambdaQueryWrapper.eq(Shenhe::getStatus,pageInfoVo.getTypeId());
        }
        if (pageInfoVo.getTjtime()!= null){//提交时间精确匹配

            shenheLambdaQueryWrapper.ge(Shenhe::getTjtime,pageInfoVo.getTjtime());
        }
        //店铺名称
        if (pageInfoVo.getSsid()!= null){
            shenheLambdaQueryWrapper.like(Shenhe::getSid,pageInfoVo.getSsid());
        }
        //审核人
        if (pageInfoVo.getShenheren() != null){
            shenheLambdaQueryWrapper.like(Shenhe::getShenheren,pageInfoVo.getShenheren());
        }
        shenheLambdaQueryWrapper.orderByDesc(Shenhe::getTjtime);
        Page<Shenhe> shenhePage1 = shMapper.selectPage(shenhePage, shenheLambdaQueryWrapper);
        return R.OK(shenhePage1);





    }

    @Override
    public R update(Shenhe shenhe, Good good) {
            shenhe.setXgtime(new Date());
            good.setXiugaitime(new Date());
            //修改商品时间
            if (shenhe.getStatus()==1){
                good.setStatus(1);
                goodMapper.updateById(good);
            }else if (shenhe.getStatus()==0){
        good.setStatus(0);
            }


        //修改
        shMapper.updateById(shenhe);
        return R.OK();

    }
@Autowired
    GoodMapper goodMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Override
    public R ist2(PageInfoVo pageInfoVo) {
        Page<Good> goodPagePage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<Good> goodLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //4、、、、、、
        if (pageInfoVo.getNumber()!= null){  //根据商品ID
            goodLambdaQueryWrapper.eq(Good::getNumber,pageInfoVo.getNumber());
        }
        if (pageInfoVo.getSname() != null){ //根据商品名称
            goodLambdaQueryWrapper.like(Good::getZhongwen,pageInfoVo.getSname());
        }
        if (pageInfoVo.getFenlei() != null){ //商品类型
            goodLambdaQueryWrapper.like(Good::getFenlei,pageInfoVo.getFenlei());
        }
        if (pageInfoVo.getChuangtime() != null){ //创建时间
            goodLambdaQueryWrapper.eq(Good::getTijiaotime,pageInfoVo.getChuangtime());
        }

        if (pageInfoVo.getStatuss()!= null){//审核状态精确匹配
            goodLambdaQueryWrapper.eq(Good::getStatus,pageInfoVo.getStatuss());
        }
        //店铺名称、
        if (pageInfoVo.getDianpuname()!= null){
            goodLambdaQueryWrapper.like(Good::getSid,pageInfoVo.getDianpuname());
        }
        //审核人模糊查询
        if (pageInfoVo.getShenheren()!= null){
            goodLambdaQueryWrapper.like(Good::getShenheren,pageInfoVo.getShenheren());
        }
        //价格区间
    if (pageInfoVo.getMaxPrice() != null && pageInfoVo.getMinPrice() != null){
        goodLambdaQueryWrapper.between(Good::getPrice,pageInfoVo.getMinPrice(),pageInfoVo.getMaxPrice());
    }
        if (pageInfoVo.getMaxsumm() != null && pageInfoVo.getMinsumm() != null){  //、根据价格与销量区间查询
            goodLambdaQueryWrapper.between(Good::getCount,pageInfoVo.getMinsumm(),pageInfoVo.getMaxsumm());
        }

        Page<Good> goodPage = goodMapper.selectPage(goodPagePage, goodLambdaQueryWrapper);
        return R.OK(goodPage);
    }

    @Override
    public R shangchuan(MultipartFile file) {
          // 上传文件
         String originalFilename = file.getOriginalFilename();
                 String extName = FileNameUtil.extName(originalFilename);
                 try {
                     StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
                     String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
                     return R.OK(url);
                 } catch (Exception e) {
                     e.printStackTrace();
                     return R.ERROR(500, "上传失败");
                 }

    }

    @Override
    public R add(Good good) {//添加
        goodMapper.insert(good);
        return R.OK();

    }

    @Override
    public R update1(Good good) {//修改
        good.setXiugaitime(new Date());
        goodMapper.updateById(good);
        return R.OK();
    }

    @Override
    public R delete(Integer id) {//删除

        goodMapper.deleteById(id);
        return R.OK();
    }
}
